﻿namespace LDM.Framework.NormalizeActionResult;

/// <summary>
/// 成功失败的枚举
/// </summary>
public enum ResultTypeEnum
{
    /// <summary>
    /// 成功，值为1。
    /// </summary>
    Success = 1,

    /// <summary>
    /// 验证失败，值为2。
    /// </summary>
    ValidateError = 2,

    /// <summary>
    /// 错误，已知的错误，值为3。
    /// </summary>
    Error = 3,

    /// <summary>
    /// 错误，写在try catch中的错误,值为4。
    /// </summary>
    Exception = 4,
}





/// <summary>
/// 结果实体
/// </summary>
/// <typeparam name="TEntity">The type of the t entity.</typeparam>
public class ResultData
{
    /// <summary>
    /// Gets or sets the type of the result，<see cref="ResultTypeEnum"/>
    /// </summary>
    /// <value>The type of the result.</value>
    public int ResultType { get; set; }

    /// <summary>
    /// Gets or sets the message.
    /// </summary>
    /// <value>The message.</value>
    public string? Message { get; set; }

    /// <summary>
    /// Gets or sets the return entity.
    /// </summary>
    /// <value>The return entity.</value>
    public object? Entity { get; set; }

    /// <summary>
    /// Initializes a new instance of the <see cref="ResultData{TEntity}"/> class.
    /// </summary>
    /// <param name="resultType">Type of the result.</param>
    /// <param name="message">The message.</param>
    /// <param name="entity">The return entity.</param>
    public ResultData(ResultTypeEnum resultType, string? message, object? entity)
    {
        ResultType = (int)resultType;
        Message = message;
        Entity = entity;
    }

    /// <summary>
    /// Successes the specified message.
    /// </summary>
    /// <param name="message">The message.</param>
    /// <param name="entity">The entity.</param>
    /// <returns>ResultData&lt;TEntity&gt;.</returns>
    public static ResultData Success(string? message, object? entity)

    {
        return GetInstance(ResultTypeEnum.Success, message, entity);
    }

    ///// <summary>
    ///// Successes the specified entity.
    ///// </summary>
    ///// <param name="entity">The entity.</param>
    ///// <returns>ResultData&lt;TEntity&gt;.</returns>
    //public static ResultData Success<TEntity>(TEntity? entity)
    //{
    //    return GetInstance(ResultTypeEnum.Success, null, entity);
    //}

    /// <summary>
    /// Successes the specified entity.
    /// </summary>
    /// <param name="entity">The entity.</param>
    /// <returns>ResultData&lt;TEntity&gt;.</returns>
    public static ResultData Success(object? entity = null)
    {
        return GetInstance(ResultTypeEnum.Success, null, entity);
    }

    /// <summary>
    /// Errors the specified message.
    /// </summary>
    /// <param name="message">The message.</param>
    /// <param name="entity">The entity.</param>
    /// <returns>ResultData&lt;TEntity&gt;.</returns>
    public static ResultData Error(string message, object? entity)
    {
        return GetInstance(ResultTypeEnum.Error, message, entity);
    }

    /// <summary>
    /// Exceptions the specified message.
    /// </summary>
    /// <param name="message">The message.</param>
    /// <returns>ResultData&lt;TEntity&gt;.</returns>
    public static ResultData Exception(string message)
    {
        return GetInstance(ResultTypeEnum.Exception, message, default);
    }

    /// <summary>
    /// Validates the error.
    /// </summary>
    /// <param name="message">The message.</param>
    /// <param name="entity">The entity.</param>
    /// <returns>ResultData&lt;TEntity&gt;.</returns>
    public static ResultData ValidateError(string message, object? entity)
    {
        return GetInstance(ResultTypeEnum.ValidateError, message, entity);
    }


    /// <summary>
    /// Gets the instance.
    /// </summary>
    /// <param name="resultType">Type of the result.</param>
    /// <param name="message">The message.</param>
    /// <param name="entity">The entity.</param>
    /// <returns>ResultData&lt;TEntity&gt;.</returns>
    public static ResultData GetInstance(ResultTypeEnum resultType, string? message, object? entity)
    {
        return new ResultData(resultType, message, entity);
    }
}

